import {PluginCommand} from "../PluginCommand";
import {flatBeltDriveCommand} from "./FlatBeltDriveCommand";
import {vBeltDriveCommand} from "./VBeltDriveCommand";
import {beltDriveModelCommand} from "./BeltDriveModelCommand";

class BeltDriveCommand extends PluginCommand{
  constructor() {
    super();
    this.pointList = [];
    this.radian = 0;
    this.deep = 0;
  }
  sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  //平带传动
  async createFlatBeltDrive(beltDriveParams,cb = () => {}) {
    await this.command.execute(true, "print(1);");
    let query = this.query;
    let command = this.command;
    let datum = this.command.datum;
    let solid = this.command.solid;
    //获取带点
    let list = beltDriveModelCommand.getBeltPoints(beltDriveParams, beltDriveParams.singleBeltThickness);
    this.pointList = list.points;
    this.deep = list.deep;
    this.radian = list.radian;
    //平带参数初始化
    flatBeltDriveCommand.constructor(query, command, this.pointList, this.deep, this.radian);
    //带轮
    let sketchNumber = 1;
    let result = {};
    //主动轮
    result = await flatBeltDriveCommand.createFlatWheel(beltDriveParams, sketchNumber, 1);
    if(!result) {
      cb();
      return;
    }
    sketchNumber = result.no;
    //从动轮
    result = await flatBeltDriveCommand.createFlatWheel(beltDriveParams, sketchNumber, 2);
    if(!result) {
      cb();
      return;
    }
    sketchNumber = result.no;
    //带
    await flatBeltDriveCommand.createFlatBelt(beltDriveParams, sketchNumber++);

    cb(); //加载完毕
  }

  //V带传动
  async createVBeltDrive(beltDriveParams,cb = () => {}) {
    await this.command.execute(true, "print(1);");
    let query = this.query;
    let command = this.command;
    let datum = this.command.datum;
    let solid = this.command.solid;
    //获取带点
    let list = beltDriveModelCommand.getBeltPoints(beltDriveParams, beltDriveParams.normalVBelt.h);
    this.pointList = list.points;
    this.deep = list.deep;
    this.radian = list.radian;
    //V带参数初始化
    vBeltDriveCommand.constructor(query, command, this.pointList, this.deep, this.radian);
    //带轮建模
    let sketchNumber = 1;
    let numHole = 4;
    let circularNumber1 = 1;
    let result1 = {};
    let result2 = {};
    //主动轮
    result1 = await vBeltDriveCommand.createVWheel(beltDriveParams, sketchNumber, 1);
    if(!result1) {
      cb();
      return;
    }
    sketchNumber = result1.no;

    //从动轮
    result2 = await vBeltDriveCommand.createVWheel(beltDriveParams, sketchNumber, 2);
    if(!result2) {
      cb();
      return;
    }
    sketchNumber = result2.no;

    //创建基准面1
    let sketchDatumFeature1 = await beltDriveModelCommand.creatLoftSketchDatum1(datum, parseFloat(beltDriveParams.normalVWheel.f));
    //创建基准面2、3、4、5
    let sketchDatumFeature2 = await beltDriveModelCommand.creatLoftSketchDatum(datum, this.pointList);

    //创建主动轮槽轮草图
    let sketchFeature3 = await vBeltDriveCommand.createAddVSketch1(sketchDatumFeature2[0], beltDriveParams, sketchNumber++);
    //主动轮旋转切除
    let revolveCutFeature1 = await beltDriveModelCommand.revolveCutAuxiliary(query, solid, sketchFeature3, result1.feature, beltDriveParams, 0, 0, 1, 360, 1);
    //轮槽个数
    await beltDriveModelCommand.linearPatternFeatures(solid, revolveCutFeature1, beltDriveParams);

    //创建从动轮槽轮草图
    let sketchFeature4 = await vBeltDriveCommand.createAddVSketch1(sketchDatumFeature2[2], beltDriveParams, sketchNumber++);
    //从动轮旋转切除
    let revolveCutFeature2 = await beltDriveModelCommand.revolveCutAuxiliary(query, solid, sketchFeature4, result2.feature, beltDriveParams, 0, 0, 1, 360, 2);
    //轮槽个数
    await beltDriveModelCommand.linearPatternFeatures(solid, revolveCutFeature2, beltDriveParams);

    //创建主动轮传动带基准面草图
    let sketchFeature5 = await vBeltDriveCommand.createAddVBeltSketch1(sketchDatumFeature2[0], beltDriveParams, sketchNumber++);
    let sketchFeature6 = await vBeltDriveCommand.createAddVBeltSketch2(sketchDatumFeature2[0], beltDriveParams, sketchNumber++);
    let sketchFeature7 = await vBeltDriveCommand.createAddVBeltSketch3(sketchDatumFeature2[1], beltDriveParams, sketchNumber++);
    let sketchFeature8 = await vBeltDriveCommand.createAddVBeltSketch4(sketchDatumFeature2[1], beltDriveParams, sketchNumber++);
    let sketchFeature9 = await vBeltDriveCommand.createAddVBeltSketch1(sketchDatumFeature2[2], beltDriveParams, sketchNumber++);
    let sketchFeature10 = await vBeltDriveCommand.createAddVBeltSketch2(sketchDatumFeature2[2], beltDriveParams, sketchNumber++);

    //创建带草图
    let sketchFeature11 = await vBeltDriveCommand.createAddBeltSketch(sketchDatumFeature1, beltDriveParams, beltDriveParams.normalVBelt.h, sketchNumber++);

    //创建带拉伸
    let extrudeFeature1 = null;
    for(let i = 0; i < sketchFeature11?.length; i++) {
      extrudeFeature1 = await beltDriveModelCommand.extrudeAddendum(query, solid, sketchFeature11[i], 4, beltDriveParams.normalVBelt.b);  // 创建拉伸
    }

    //带上部分拉伸切除
    let extrudeCutFeature3 = await beltDriveModelCommand.extrudeCutAddendum(query, solid, sketchFeature5, extrudeFeature1, 0, 0, parseFloat(beltDriveParams.distance) * Math.sin(this.radian));
    let extrudeCutFeature4 = await beltDriveModelCommand.extrudeCutAddendum(query, solid, sketchFeature6, extrudeCutFeature3, 0, 0, parseFloat(beltDriveParams.distance) * Math.sin(this.radian));
    //带上圆弧部分旋转切除
    let revolveCutFeature3 = await beltDriveModelCommand.revolveCutAuxiliary(query, solid, sketchFeature5, extrudeCutFeature4, beltDriveParams, 0, 0, 1, 360 - (this.radian / Math.PI) * 360, 1);
    let revolveCutFeature4 = await beltDriveModelCommand.revolveCutAuxiliary(query, solid, sketchFeature6, revolveCutFeature3, beltDriveParams, 0, 0, 1, 360 - (this.radian / Math.PI) * 360, 1);
    //带下部分拉伸切除
    let extrudeCutFeature5 = await beltDriveModelCommand.extrudeCutAddendum(query, solid, sketchFeature7, revolveCutFeature4, 1, 0, parseFloat(beltDriveParams.distance) * Math.sin(this.radian));
    let extrudeCutFeature6 = await beltDriveModelCommand.extrudeCutAddendum(query, solid, sketchFeature8, extrudeCutFeature5, 1, 0, parseFloat(beltDriveParams.distance) * Math.sin(this.radian));
    //带下圆弧部分旋转切除
    let revolveCutFeature5 = await beltDriveModelCommand.revolveCutAuxiliary(query, solid, sketchFeature9, extrudeCutFeature6, beltDriveParams, 0, 0, 1, this.radian / Math.PI * 360, 2);
    let revolveCutFeature6 = await beltDriveModelCommand.revolveCutAuxiliary(query, solid, sketchFeature10, revolveCutFeature5, beltDriveParams, 0, 0, 1, this.radian / Math.PI * 360, 2);
    //带根数
    await beltDriveModelCommand.linearPatternEntity(query, solid, revolveCutFeature6, beltDriveParams);

    //加载完毕
    cb();
  }

  async closeAllAlert() {
    if (this.event) {
      this.event.closeAllAlert();
    }
  }
}

const beltDriveCommand = new BeltDriveCommand();
export {beltDriveCommand};
